<template>
  <div class="jaguar-view">
    <div class="jaguar-search">
      <el-form :inline="true" :model="searchForm" @submit.prevent>
        <search-input
          label="用户信息"
          v-model="searchForm.fuzzyUserInfo"
          :search="getData"
        />
        <search-select
          label="是否启用"
          v-model="searchForm.userEnable"
          :options="isEnableOptions"
          :search="getData"
        />
        <search-select
          label="是否锁定"
          v-model="searchForm.userLocked"
          :options="[
            { label: '正常', value: false },
            { label: '锁定', value: true },
          ]"
          :search="getData"
        />
        <el-form-item label="用户部门">
          <dept-tree-select
            v-model="searchForm.userDeptIds"
            selectionMode="multiple"
            placeholder="请选择用户部门"
            @change="getData(1)"
          />
        </el-form-item>

        <el-form-item>
          <search-button :search="getData" />
          <reset-button :search-form="searchForm" :search="getData" />
        </el-form-item>
      </el-form>
    </div>

    <div class="jaguar-control">
      <add-button @click="handleCreate" />

      <Button
        label="批量编辑"
        icon="pi pi-file-edit"
        @click="handleBatchEdit"
      />
    </div>

    <Card>
      <template #content>
        <DataTable
          ref="table"
          :value="tableData"
          v-model:selection="selectedRows"
          selectionMode="multiple"
          dataKey="id"
          stripedRows
          :loading="loading"
        >
          <Column selectionMode="multiple" />
          <Column field="userAccount" header="用户账号">
            <template #body="scope">
              {{ scope.data.userAccount }}
              <span v-if="scope.data.userLocked" style="color: red">
                （已锁定）
              </span>
            </template>
          </Column>
          <Column field="userPhone" header="用户手机号" />
          <Column field="userEmail" header="用户邮箱" />
          <Column field="userNickName" header="用户昵称" />
          <Column header="用户角色">
            <template #body="scope">
              <template v-if="scope.data.roleNames">
                <Tag
                  v-for="roleName in scope.data.roleNames.split(',')"
                  :key="roleName"
                >
                  {{ roleName }}
                </Tag>
              </template>
            </template>
          </Column>
          <Column field="dept.deptName" header="用户部门">
            <template #body="scope">
              <Tag v-if="scope.data.userDeptId == 0" type="danger"
                >最高管理部</Tag
              >
              <Tag v-else-if="scope.data.deptName">
                {{ scope.data.deptName }}
              </Tag>
            </template>
          </Column>
          <Column field="userEnable" header="用户是否启用">
            <template #body="scope">
              <ToggleSwitch v-model="scope.data.userEnable" disabled />
            </template>
          </Column>
          <Column header="操作" width="220">
            <template #body="scope">
              <view-button @click="handleView(scope.data)" />
              <edit-button @click="handleEdit(scope.data)" />
              <delete-button
                v-if="!scope.data.userBuiltIn"
                @confirm="handleDelete(scope.data)"
              />
            </template>
          </Column>
        </DataTable>

        <table-page :page="page" :search="getData" />
      </template>
    </Card>

    <Edit :dialog="dialog" @success="getData" />

    <batch-edit :dialog="batchEditDialog" @success="getData" />
  </div>
</template>
<script>
import { fetchList, del } from '@/api/upms/user'
import Edit from './Edit'
import BatchEdit from './BatchEdit'
import { isEnable } from '@/components/dictionary/index.js'

export default {
  name: 'User',
  data() {
    return {
      loading: false,
      tableData: [],
      searchForm: {},
      selectedRows: [],
      page: {
        total: 0,
        current: 1,
        size: 10,
      },
      dialog: {
        entity: null,
        operation: null,
        visible: false,
      },
      batchEditDialog: {
        userIds: null,
        visible: false,
      },
      isEnableOptions: isEnable,
    }
  },
  components: {
    Edit,
    BatchEdit,
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData(current, size) {
      if (current) this.page.current = current
      if (size) this.page.size = size

      this.loading = true
      fetchList(
        Object.assign(
          {
            current: this.page.current,
            size: this.page.size,
          },
          this.searchForm
        )
      )
        .then((res) => {
          this.tableData = res.data.data.records
          this.page.total = res.data.data.total
        })
        .finally(() => {
          this.loading = false
        })
    },
    handleCreate() {
      this.dialog.entity = null
      this.dialog.operation = 1
      this.dialog.visible = true
    },
    handleEdit(row) {
      this.dialog.entity = row
      this.dialog.operation = 2
      this.dialog.visible = true
    },
    handleView(row) {
      this.dialog.entity = row
      this.dialog.operation = 3
      this.dialog.visible = true
    },
    handleDelete(row) {
      del(row.id).then(() => {
        this.$message.success('删除成功')
        this.getData()
      })
    },
    handleBatchEdit() {
      let rows = this.$refs.table.getSelectionRows()
      if (rows == null || rows.length == 0) {
        this.$message.warning('请选择需要编辑的用户')
        return
      }

      let userIds = []
      for (let i in rows) {
        userIds.push(rows[i].id)
      }
      this.batchEditDialog.userIds = userIds
      this.batchEditDialog.visible = true
    },
  },
}
</script>
